package com.cqeec.myblog.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public final class DbUtil {
	// 单实例(数据库连接对象)
	private static Connection connection;

	private DbUtil() {
	}

	/**
	 * 获取数据库连接对象。
	 * 
	 * @return 数据库连接对象。
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public static Connection getDbConnection() throws ClassNotFoundException, SQLException {
		if (connection == null) {
			// 加载数据库驱动类
			Class.forName("com.mysql.jdbc.Driver");
			// 数据库JDBC连接串
			String url = "jdbc:mysql://localhost:3306/MyBlog?useUnicode=true&characterEncoding=utf-8&autoReconnect=true";
			// 通过驱动管理器获取数据库连接对象
			connection = DriverManager.getConnection(url, "root", "");
		}
		return connection;
	}

}
